﻿
// CoreGLView.h: CCoreGLView 类的接口
//
#include <GL/freeglut.h>
#pragma once
#include "Textures.h"
#define MAX_NO_TEXTURES			5
#define PI						3.141592654
#define DEG2RAD(angle)			angle * (PI / 180)	// 将角度转换为弧度
#define ROTATION_SPEED			10  // 此参数定义一步相当于几分钟

#define EARTH1	0
#define EARTH2	1
#define EARTH3	2
#define EARTH4	3
#define EARTH5	4
#define NUM_PLANETS	5
class CPlanet
{
public:
	float m_size, m_sections;
	float m_posX, m_posY, m_posZ;
	float m_SolarAngle, m_OwnAxisAngle;
	int	  m_TextureID;
	float m_SolarT, m_selfT;	// 公转和自转周期，单位：天
public:
	void SetPara(float size, float sections, float posX, float posY, float posZ, float SolarAngle, float OwnAxisAngle, int TextureID, float selfT, float SolarT)
	{
		m_size = size, m_sections = sections;
		m_posX = posX, m_posY = posY, m_posZ = posZ;
		m_SolarAngle = SolarAngle, m_OwnAxisAngle = OwnAxisAngle;
		m_TextureID = TextureID;
		m_SolarT = SolarT, m_selfT = selfT;
	}
	void Rotate(float time)
	{	// 按给定时间调整行星位置和角度，time单位为分钟
		time /= (24 * 60); // 转化为周期单位：天
		// solar rotation

		// axis rotation
		m_OwnAxisAngle += (time / m_selfT) * 360;
		if (m_OwnAxisAngle >= 360)
			m_OwnAxisAngle -= 360;
		else if (m_OwnAxisAngle < 0)
			m_OwnAxisAngle += 360;
	}
	
	void Draw()
	{
		glPushMatrix();
		
		glRotatef(m_SolarAngle, 0.0f, -1.0f, 0.0f);
		glTranslatef(m_posX, m_posY, m_posZ);
		glRotatef(m_OwnAxisAngle, 0.0f, -1.0f, 0.0f);
		glRotatef(90.0f, 1.0f, 0.0f, 0.0f);

		GLUquadricObj*  q = gluNewQuadric();
		gluQuadricDrawStyle(q, GLU_FILL);
		gluQuadricNormals(q, GLU_SMOOTH);
		gluQuadricTexture(q, GL_TRUE);
		gluSphere(q, m_size, m_sections, m_sections);
		gluDeleteQuadric(q);

		glPopMatrix();
	}
};
class CCoreGLView : public CView
{
protected: // 仅从序列化创建
	CCoreGLView() noexcept;
	DECLARE_DYNCREATE(CCoreGLView)

// 特性
public:
	CCoreGLDoc* GetDocument() const;

// 操作
public:

// 重写
public:
	virtual void OnDraw(CDC* pDC);  // 重写以绘制该视图
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// 实现
public:
	virtual ~CCoreGLView();
	BOOL RenderScene();
	BOOL SetupPixelFormat(void);
	BOOL InitializeOpenGL(CDC* pDC);

	HGLRC		m_hRC;			//OpenGL绘制描述表
	HPALETTE	m_hPalette;		//OpenGL调色板
	CDC*	    m_pDC;			//OpenGL设备描述表
	float m_viewdist;
	float m_viewdir;
	float m_viewht;
	int draging = 0;
	int lastx, lasty;
	
	int iCometAngle, iCometPos;
	//CTextures m_Textures;
	//CPlanet	m_planet[NUM_PLANETS];
	void init(void);
	void InitPlanets();
#ifdef _DEBUG
	void UpdateLook();
	void UpdateViewDir(CPoint point);
	void rotate();
	void DrawSun();
	void DrawPlanets();
	GLuint texture_id[MAX_NO_TEXTURES];
	CPlanet	m_planet[NUM_PLANETS];
	Textures m_Textures;
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// 生成的消息映射函数
protected:
	DECLARE_MESSAGE_MAP()
protected:
//	virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnDestroy();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnTimer(UINT_PTR nIDEvent);
public:
	afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};

#ifndef _DEBUG  // CoreGLView.cpp 中的调试版本
inline CCoreGLDoc* CCoreGLView::GetDocument() const
   { return reinterpret_cast<CCoreGLDoc*>(m_pDocument); }
#endif

